# -*- coding: UTF-8 -*-
import os
import json

# 指定文件夹路径
folder_path = os.path.dirname(__file__)  # 替换为你的文件夹路径


def add_format_to_json(folder_path):
    # 遍历文件夹下所有文件
    for filename in os.listdir(folder_path):
        if filename.endswith(".json") and len(filename) >= 10 and filename[-10] != "F" and filename[-11] != "3" and filename[-10] != "E":
            file_path = os.path.join(folder_path, filename)
            # 读取JSON文件
            with open(file_path, "r") as file:
                data = json.load(file)
                # 检查是否存在format字段和jointnum字段，如果不存在则添加
                if data.has_key("format") == False:
                    print(file_path + " add format")
                    data["format"] = True
                if data.has_key("jointnum") == False:
                    print(file_path + " add jointnum")
                    data["jointnum"] = "6"
                # if data.has_key('format') == True:
                #     print(file_path + " del format")
                #     del data['format']
                # if data.has_key('jointnum') == True:
                #     print(file_path + " del jointnum")
                #     del data['jointnum']

            # 写入更新后的JSON文件
            with open(file_path, "w") as file:
                json.dump(data, file, ensure_ascii=False, indent=4)  # indent=4 用于格式化输出，可选项


if __name__ == "__main__":
    add_format_to_json(folder_path + "/../SERVO")
    add_format_to_json(folder_path + "/../CONTROLLER")
